package goodweather.cjxy.edu.util;

import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by 26706 on 2018/3/24.
 * 封装的okhttp
 */

public class HttpUtil {

    public static void sendOkHttpRequest(String address, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(address).build();
        client.newCall(request).enqueue(callback);//把结果传给 回调接口
    }


    /**
     * 发送post请求
     * @param address
     * @param list
     * @param macAdd
     * @throws Exception
     */
    public static void sendPost(String address, List<String> list, String macAdd,okhttp3.Callback callback) throws Exception{
        OkHttpClient client = new OkHttpClient();
        FormBody formBody=null;
        FormBody.Builder builder=new FormBody.Builder();
        String t="";
        for(String str:list){
           t+=str+":";
        }
        builder.add("macaddress",macAdd);
        builder.add("uphone",t);//拼接参数
        formBody=builder.build();

        Request request=new Request.Builder().
                post(formBody).
                url(address).
                build();//构建请求

        //发送请求
        client.newCall(request).enqueue(callback);//把结果传给 回调接口
    }

    public static void sendPost(String address, List<String>list,String macAdd,Map<String,String> map,okhttp3.Callback callback){
        OkHttpClient client = new OkHttpClient();
        FormBody formBody=null;
        FormBody.Builder builder=new FormBody.Builder();
        String tmp="";

        for(String str:list){
           tmp+=str+":";
        }

        builder.add("macadd",macAdd);//网卡
        builder.add(map.get("umsg"),tmp);//短信
        formBody=builder.build();

        Request request=new Request.Builder().
                post(formBody).
                url(address).
                build();//构建请求

        client.newCall(request).enqueue(callback);//发送请求

    }
}